<?php
/**
 * @desc 首页管理
 * @author kevin.wang
 * @date 2015-01-01 12:00:00
 */
class SiteController extends Controller
{
	protected $params = array();
	 public function actions()
    { 
            return array( 
                    //验证码
                    'captcha'=>array(
                            'class'=>'Captcha',
                            'backColor'=>0x778899, 
                            'maxLength'=>'4',       // 最多生成几个字符
                            'minLength'=>'2',       // 最少生成几个字符
                            'height'=>'40',
							//'fixedVerifyCode' => substr(md5(time()),11,4),
							'testLimit'=>0
                    ), 
            ); 
    }
	//过滤器
	public function filters()
    {
        return array(
            'UserLogin + index',
        );
    }

	//首页
	public function actionIndex()
	{
		//用户信息
		$userInfo = json_decode(Yii::app()->session['userinfo'], true);
		$model = new Site;
		$sysInfo = $model->getSysInfo();
		$this->layout = '//layouts/main';
		$this->params = $userInfo;
		$this->render('index', array(
				'userInfo' => $userInfo,
				'sysInfo' => $sysInfo
			));
	}

	//错误页面
	public function actionError()
	{
		if($error=Yii::app()->errorHandler->error)
		{
			if(Yii::app()->request->isAjaxRequest)
				echo $error['message'];
			else
				$this->render('error', $error);
		}
	}

	//登录页面
	public function actionLogin()
	{
		$model = new LoginForm;
		if($model->userIsLogin()){
			$this->redirect(Yii::app()->createUrl("site/index"));
			Yii::app()->end();
		}
		$this->render('login');
	}
	//登录框
	public function actionLoginDialog(){
		$this->render('loginDialog');
	}
	//登录
	public function actionDoLogin(){
		if(!Yii::app()->request->isAjaxRequest){
			$this->output['status'] = 10002;
			$this->renderJson($this->output);
		}
		$model = new LoginForm;
		$model->attributes = Yii::app()->request->getParam("loginForm");
		if(!$model->validate() || !$model->login()){
			$msg = array_values($model->getErrors());
			$this->output['status'] = 10000;
			$this->output['msg'] = $msg[0];
			$this->renderJson($this->output);
		}
		$this->output['code'] = 200;
		$this->output['callbackType'] = 'closeCurrent';
		$this->output['navTabId'] = Yii::app()->request->getParam("refushTarget");
		$this->renderJson($this->output);
	}

	//退出登录
	public function actionLogout()
	{
		Yii::app()->user->logout();
		$this->redirect(Yii::app()->homeUrl);
	}
}